package com.skywalker.j2cache;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.skywalker.common.model.Account;
import com.skywalker.common.plugin.j2cache.CacheName;
import com.skywalker.common.plugin.j2cache.EvictInterceptor;
import com.skywalker.common.plugin.j2cache.J2CacheKit;

import java.util.UUID;

/**
 * @user: skywalker
 * @date: 2019/4/13 10:24
 * @description:
 */
public class J2CacheController extends Controller {

    @Inject
    J2CacheService j2CacheService;

    public void addToCache(String key, String val) {
        String cacheName = "j2cache_demo";
        if (StrKit.isBlank(key)) {
            key = "key" + UUID.randomUUID().toString();
        }
        if (StrKit.isBlank(val)) {
            val = "高阁客竟去，小园花乱飞。参差连曲陌，迢递送斜晖。";
        }
        J2CacheKit.put(cacheName, key, val);
        renderText(J2CacheKit.get(cacheName, key));
    }

    public void getAccountInfo(String name) {
        renderJson(j2CacheService.getAccountInfo(name));
    }

    @CacheName({Account.CACHE_NAME, "test"})
    @Before(EvictInterceptor.class)
    public void saveAccount(String nickName) {
        renderJson(j2CacheService.saveAccount(nickName));
    }
}
